package com.example.demo.controller;

import com.example.demo.config.MqttBroker;
import com.example.demo.entity.*;
import com.example.demo.service.*;
import com.example.demo.util.GetMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/step")
@RequiredArgsConstructor
@Component
public class StepsController {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private final CodeHistoryService codeHistoryService;
    private final CodeAssociationService codeAssociationService;
    private final CodingRulesService codingRulesService;
    private final CodeAssignmentRulesService codeAssignmentRulesService;
    private final UserProductinfoService userProductinfoService;
    private final ProductStoreService productStoreService;
    private final PurchaseApplyOrderService purchaseApplyOrderService;
    private final PurchaseOrderService purchaseOrderService;
    private final ProducePlanService producePlanService;
    private final MaterialPlanService materialPlanService;
    private final LogisticsOrderService logisticsOrderService;
    private final DeliveryOrderService deliveryOrderService;
    private final DispatchInfoService dispatchInfoService;

    //orderstate 1下单 2付款 3卖家签字 4收货 5入库
    //           1下单 2付款 3卖家签字 4入库 5用户入库
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/resetstep")
    public GetMessage resetStep (String appId,String stepId ,String userId, String trainingId ,Integer isAll) throws JsonProcessingException, MqttException {
        //String userId=token;
        Map<String,Object> map=new HashMap<>();
        PurchaseOrder purchaseOrder=null;
        Integer step=Integer.parseInt(stepId);
        if(isAll==0){
            switch (step){
                case 1:
                    purchaseApplyOrderService.delete(userId,trainingId);
                    map.put("targetPageId","applySave");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    if(purchaseOrder!=null){
                        purchaseOrderService.deleteById((Integer) purchaseOrder.getId());
                    }

                    break;

                case 2:
                    map.put("targetPageId","confirmOrder");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    if(purchaseOrder!=null){
                        PurchaseOrder p=purchaseOrder;
                        p.setOrderstate("2");
                        p.setIssignseller("0");
                        purchaseOrderService.updateById(p);
                    }

                    break;
                case 3:
                    map.put("targetPageId","orderPrint");
                    break;
                case 4:
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    if(purchaseOrder!=null){
                        PurchaseOrder p=purchaseOrder;
                        p.setOrderstate("1");
                        p.setIspayment("0");
                        p.setPaymentaccount("");
                        p.setAccountname("");
                        purchaseOrderService.updateById(p);
                    }

                    map.put("targetPageId","payMent");
                    break;
                case 5:
                    map.put("targetPageId","printOrder");
                    break;

                case 6:
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    ProducePlan producePlan=producePlanService.selectByInfo(null,null,null,purchaseOrder.getOrdernumber(),null);
                    if(producePlan!=null){
                        producePlanService.deleteById((Integer) producePlan.getId());
                    }
                    map.put("targetPageId","producePlan");
                    break;

                case 7:
                    map.put("targetPageId","produceInfo");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    ProducePlan producePlan1=producePlanService.selectByInfo(null,null,null,purchaseOrder.getOrdernumber(),null);
                    MaterialPlan materialPlan=materialPlanService.selectByInfo(producePlan1.getPlannumber());
                    if(materialPlan!=null){
                        materialPlanService.deleteById((Integer) materialPlan.getId());
                    }
                    break;

                case 8:
                    map.put("targetPageId","produceInfo");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    if(purchaseOrder!=null){
                        purchaseOrderService.deleteById((Integer) purchaseOrder.getId());
                    }

                    break;
                case 9:
                    map.put("targetPageId","partOrderPrint");
                    break;
                case 10:
                    map.put("targetPageId","showOrder");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    if(purchaseOrder!=null){
                        logisticsOrderService.delete(userId,trainingId,purchaseOrder.getOrdernumber());
                    }

                    break;
                case 11:
                    map.put("targetPageId","logisticsOrderInfo");
                    break;
                case 12:
                    map.put("targetPageId","");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    LogisticsOrder l1=logisticsOrderService.selectByInfo(null,null,null,null,purchaseOrder.getOrdernumber());
                    if(l1!=null){
                        dispatchInfoService.delete(userId,trainingId,l1.getLogisticsnumber());
                    }

                    break;
                case 13:
                    map.put("targetPageId","vehicleDispatchInfo");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    LogisticsOrder l2=logisticsOrderService.selectByInfo(null,null,null,null,purchaseOrder.getOrdernumber());
                    DispatchInfo d=dispatchInfoService.select(l2.getLogisticsnumber());
                    if(d!=null){
                        DispatchInfo d1=d;
                        d1.setDriverreport("0");
                        d1.setCarreport("0");
                        dispatchInfoService.updateById(d1);
                    }

                    break;

                case 14:
                    map.put("targetPageId","");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    LogisticsOrder l=logisticsOrderService.selectByInfo(null,null,null,null,purchaseOrder.getOrdernumber());
                    if(l!=null){
                        deliveryOrderService.delete(userId,trainingId,l.getLogisticsnumber());
                    }

                    break;

                case 15:
                    map.put("targetPageId","checkOrder");
                    break;
                case 16:
                    map.put("targetPageId","checkGoods");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    PurchaseOrder p=purchaseOrder;
                    p.setOrderstate("3");
                    purchaseOrderService.updateById(p);
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    ProductStore productStore=productStoreService.select(null,purchaseOrder.getOrdernumber());
                    if(productStore!=null){
                        productStoreService.deletById((Integer) productStore.getId());
                    }
                    break;
                case 171:
                    map.put("targetPageId","monthBill");
                    break;
                case 172:
                    map.put("targetPageId","monthBillCheck");
                    break;
                case 173:
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"1");
                    if(purchaseOrder!=null){
                        PurchaseOrder p4=purchaseOrder;
                        p4.setIspayment("0");
                        p4.setOrderstate("1");
                        p4.setPaymentaccount("");
                        p4.setAccountname("");
                        purchaseOrderService.updateById(p4);
                    }
                    map.put("targetPageId","payMentPart");
                    break;
                case 18:
                    map.put("targetPageId","materialDemand");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    ProducePlan producePlan2=producePlanService.selectByInfo(null,null,null,purchaseOrder.getOrdernumber(),null);
                    MaterialPlan materialPlan1=materialPlanService.selectByInfo(producePlan2.getPlannumber());
                    if(materialPlan1!=null){
                        MaterialPlan m=materialPlan1;
                        m.setIsreceive("0");
                        materialPlanService.updateById(m);
                    }
                    break;

                case 19:
                    map.put("targetPageId","tvAssembly");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    LogisticsOrder logisticsOrder19=logisticsOrderService.selectByInfo(null,null,null,null,purchaseOrder.getOrdernumber());
                    if(logisticsOrder19!=null){
                        deliveryOrderService.delete(null,null,logisticsOrder19.getLogisticsnumber());
                    }
                    if(purchaseOrder!=null){
                        ProducePlan producePlan19=producePlanService.selectByInfo(null,null,null,purchaseOrder.getOrdernumber(),null);
                        if(producePlan19!=null){
                            producePlan19.setPlanstate("1");
                            producePlan19.setProducenumber("90");
                            producePlanService.updateById(producePlan19);
                        }
                    }


                    break;
                case 20:
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    ProductStore productStore1=productStoreService.select(null,purchaseOrder.getOrdernumber());
                    if(productStore1!=null){
                        productStoreService.deletById((Integer) productStore1.getId());
                    }
                    map.put("targetPageId","tvStore");
                    break;
                case 21:
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    LogisticsOrder logisticsOrder=logisticsOrderService.selectByInfo(null,null,null,null,purchaseOrder.getOrdernumber());
                    if(logisticsOrder!=null){
                        logisticsOrderService.delete(null,null,purchaseOrder.getOrdernumber());
                    }
                    map.put("targetPageId","tvLogisticsOrder");
                    DispatchInfo deliveryOrder=dispatchInfoService.select(logisticsOrder.getLogisticsnumber());
                    if(deliveryOrder!=null){
                        dispatchInfoService.delete(null,null,logisticsOrder.getLogisticsnumber());
                    }
                    break;
                case 22:
                    map.put("targetPageId","outBound");
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    LogisticsOrder l4=logisticsOrderService.selectByInfo(null,null,null,null,purchaseOrder.getOrdernumber());
                    DispatchInfo d4=dispatchInfoService.select(l4.getLogisticsnumber());
                    if(d4!=null){
                        DispatchInfo d5=d4;
                        d5.setDriverreport("0");
                        d5.setCarreport("0");
                        dispatchInfoService.updateById(d5);
                    }

                case 23:
                    purchaseOrder=purchaseOrderService.selectByInfo(userId,trainingId,null,null,"0");
                    PurchaseOrder purchaseOrder2=purchaseOrder;
                    purchaseOrder2.setOrderstate("4");
                    purchaseOrderService.updateById(purchaseOrder2);
                    map.put("targetPageId","userStore");
                    break;
            }
        }else{
//            map.put("targetPageId","Default");
//            userProductinfoService.deleteByInfo(userId,null,trainingId);
//            codingRulesService.deleteByInfo(userId,null,trainingId);
//            codeAssignmentRulesService.deleteByInfo(userId,null,trainingId);
//            codeHistoryService.deleteByInfo(null,trainingId,userId,0);
//            codeHistoryService.deleteByInfo(null,trainingId,userId,1);
////            productStoreService.deleteByInfo(userId,null,trainingId);
//            codeAssociationService.delete(null,trainingId,userId);
        }

//        map.put("trainingId",trainingId);
//        map.put("isAll",isAll);
//        String payload = objectMapper.writeValueAsString(map);
//        MqttBroker.mqttClient.publish("ResetStep/"+appId+"/"+userId,new MqttMessage(payload.getBytes(StandardCharsets.UTF_8)));
        return new GetMessage("成功","200","");
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/putstore")
    public GetMessage putstore (String appId,String userId, String trainingId) throws JsonProcessingException, MqttException {
        //String userId=token;
        Map<String,Object> map=new HashMap<>();
        map.put("type","putStore");
        map.put("trainingId",trainingId);
        map.put("appId",appId);
        map.put("data","");
        String payload = objectMapper.writeValueAsString(map);
        MqttBroker.mqttClient.publish("StepInfo/gree/"+userId,new MqttMessage(payload.getBytes(StandardCharsets.UTF_8)));
        return new GetMessage("成功","200","");
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/sendstep")
    public GetMessage sendstep (String type,String trainingId,String userId) throws JsonProcessingException, MqttException {
        //String userId=token;
        Map<String,Object> map=new HashMap<>();
        map.put("type",type);
        map.put("trainingId",trainingId);
        String payload = objectMapper.writeValueAsString(map);
        MqttBroker.mqttClient.publish("StepInfo/supply/"+userId,new MqttMessage(payload.getBytes(StandardCharsets.UTF_8)));
        return new GetMessage("成功","200","");
    }

}
